package filters

import "log"

var allFilterMap = map[string]NamedFilter{
	"/FlateDecode": new(FlateDecodeFilter),
	"/JPXDecode":   new(JPXDecodeFilter),
}

type compositeFilter struct {
	filters []NamedFilter
}

func (this *compositeFilter) Decode(data []byte) ([]byte, error) {
	var err error
	for _, oneFilter := range this.filters {
		data, err = oneFilter.Decode(data)
		if err != nil {
			return nil, err
		}
	}
	return data, nil
}

func (this *compositeFilter) Encode(data []byte) ([]byte, error) {
	var err error
	for _, oneFilter := range this.filters {
		data, err = oneFilter.Encode(data)
		if err != nil {
			return nil, err
		}
	}
	return data, nil
}

func NewCompositeFilter(filterNames []string) NamedFilter {
	var filters = make([]NamedFilter, 0, len(filterNames))
	for _, filterName := range filterNames {
		if f, has := allFilterMap[filterName]; has {
			filters = append(filters, f)
		} else {
			log.Printf("无%s过滤器注册", filterName)
		}
	}
	return &compositeFilter{
		filters: filters,
	}
}
